package com.cincc.cinccimdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.callback.OnItemClickListener;
import com.cincc.im.model.dbmodel.GroupMemberInfo;

import java.util.ArrayList;
import java.util.List;

public class ConversationDetailMemberAdapter extends RecyclerView.Adapter<ConversationDetailMemberAdapter.MyViewHolder> {

    List<GroupMemberInfo> mList = new ArrayList<>();
    Context mContext;
    boolean enableAdd = false;
    boolean enableRemove = false;

    public ConversationDetailMemberAdapter(Context mContext, boolean enableAdd, boolean enableRemove) {
        this.mContext = mContext;
        this.enableAdd = enableAdd;
        this.enableRemove = enableRemove;
    }

    public void setList(List<GroupMemberInfo> list) {
        mList.clear();
        mList.addAll(list);
        if (enableAdd) mList.add(new GroupMemberInfo());
        if (enableRemove) mList.add(new GroupMemberInfo());
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.conversation_item_member_info, parent, false);
        return new ConversationDetailMemberAdapter.MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        GroupMemberInfo memberInfo = mList.get(position);
        if (enableAdd && enableRemove) {
            if (position == mList.size() - 1) {
                holder.nameTextView.setVisibility(View.GONE);
                holder.portraitImageView.setImageResource(R.mipmap.ic_remove_team_member);
            } else if (position == mList.size() - 2) {
                holder.nameTextView.setVisibility(View.GONE);
                holder.portraitImageView.setImageResource(R.mipmap.ic_add_team_member);
            } else {
                holder.nameTextView.setText(memberInfo.nickName);
                Glide.with(mContext).load(memberInfo.protraitUrl).placeholder(R.mipmap.avatar_def)
                        .transforms(new CenterCrop(), new RoundedCorners(15))
                        .into(holder.portraitImageView);
            }
            holder.itemView.setOnClickListener(v -> {
                if (position == mList.size() - 1) {
                    onMemberClickListener.delMemberClick();
                }
                if (position == mList.size() - 2) {
                    onMemberClickListener.addMemberClick();
                }
            });
        } else {
            if (position == mList.size() - 1) {
                holder.nameTextView.setVisibility(View.GONE);
                holder.portraitImageView.setImageResource(R.mipmap.ic_add_team_member);
            } else {
                holder.nameTextView.setText(memberInfo.nickName);
                Glide.with(mContext).load(memberInfo.protraitUrl).placeholder(R.mipmap.avatar_def)
                        .transforms(new CenterCrop(), new RoundedCorners(15))
                        .into(holder.portraitImageView);
            }
            holder.itemView.setOnClickListener(v -> {
                if (position == mList.size() - 1) {
                    onMemberClickListener.addMemberClick();
                }
            });
        }
    }

    public interface OnMemberClickListener {
        void addMemberClick();

        void delMemberClick();
    }

    private OnMemberClickListener onMemberClickListener;

    public void setOnMemberClickListener(OnMemberClickListener listener) {
        onMemberClickListener = listener;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView portraitImageView;
        TextView nameTextView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            portraitImageView = itemView.findViewById(R.id.portraitImageView);
            nameTextView = itemView.findViewById(R.id.nameTextView);
        }
    }
}
